﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Web.Libs.WeiXin.business;
using DTcms.Web.Libs.WeiXin.lib;

namespace DTcms.Web.WxSdjs
{
    public partial class Index : System.Web.UI.Page
    {
        public static string OpenId = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            string code = Request.QueryString["code"] ?? string.Empty;
            Log.Info(this.GetType().ToString(), "page load");
            if (!IsPostBack)
            {
                try
                {
                    #region 此处微信会回调传code去获取openid和access_token,可以自己封装
                    JsApiPay jsApiPay = new JsApiPay(this);
                    if (string.IsNullOrWhiteSpace(code))
                        code = jsApiPay.GetCode();

                    jsApiPay.GetOpenidAndAccessTokenFromCode(code);
                    if (jsApiPay.openid != null)
                    {
                        string openid = jsApiPay.openid;
                        OpenId = openid;
                        //Session["openid"] = openid;
                    }
                    #endregion

                    Response.Redirect("index.html?openId=" + OpenId);
                }
                catch (Exception ex)
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错，请重试" + "</span>");
                }
            }
        }
    }
}